package org.zfes.snowier.cloud.zbss.cms.origin.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.consts.AppBizConst;
import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.origin.service.ICmsOriginService;
import org.zfes.snowier.cloud.zbss.cms.origin.dao.CmsOriginMapper;
import org.zfes.snowier.cloud.zbss.cms.origin.model.CmsOrigin;
import java.util.stream.Stream;
@Service
public class CmsOriginServiceImpl implements ICmsOriginService{
 	@Autowired
	private CmsOriginMapper cmsOriginMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsOrigin(CmsOrigin cmsOrigin) {
		ZBeanUtil.validateBean(cmsOrigin);
		cmsOrigin.setStatus(Byte.valueOf("1"));
		cmsOrigin.setCreateTime(ZDateUtil.newDate());
		cmsOrigin.setUpdateTime(ZDateUtil.newDate());
		cmsOriginMapper.insertSelective(cmsOrigin);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsOrigin> loadCmsOriginById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsOriginMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsOrigin(CmsOrigin cmsOrigin) {
		ZBeanUtil.validateBean(cmsOrigin);
		cmsOrigin.setUpdateTime(ZDateUtil.newDate());
		cmsOriginMapper.updateByPrimaryKeySelective(cmsOrigin);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsOrigin(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsOriginMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsOriginStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsOriginMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsOriginDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("originName").ifPresent(v->pm.updateParam("originName", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(cmsOriginMapper.selectListCount(pm), cmsOriginMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadCmsOriginComboboVo(Boolean firstEmpty) {
		
		List<ComboboVo> cmsOriginList=cmsOriginMapper.selectComboboVo(AppBizConst.STATUS.enabled);
		List<ComboboVo> cmsOriginList2=new ArrayList<>();
		if(firstEmpty!=null&&firstEmpty){
			cmsOriginList2.add(0, ComboboVo.newEmptyFirst()); 
		}
		cmsOriginList2.addAll(cmsOriginList);
		 
		return cmsOriginList2;
	}
	
}